local BaseWnd = require(SCRIPTS_DIR .. ".wnds.BaseWnd");
local MapWnd = class("MapWnd",BaseWnd);
function MapWnd:ctor()
	MapWnd.super.ctor(self);
    self.layer = nil;
    self.guild = nil;
    self.hero = nil;
    self.m_Touch_Point = cc.p(-1,-1);
end

function MapWnd:onOpen()
    
    mgrs.animate:createAnimate({name = "sendPoint",frameName = "sendPoint_%d.png",start = 0,endIndex = 3,time = 0.5});
    mgrs.animate:createAnimate({name = "attack_stand",frameName = "1004_attack_stand_%d.png",start = 0,endIndex = 4,time = 0.5});
    mgrs.animate:createAnimate({name = "guide",frameName = "guide000%d.png",start = 0,endIndex = 2,time = 0.3});

    local sv = self:widget("sv");
    mgrs.map:setMapId(101);
    local map = mgrs.map:getMap();
    local layer = display.newLayer();
    layer:setContentSize(map:getContentSize());
    layer:addChild(map);
    sv:addScrollNode(layer);
    self.layer = layer;
    
    local getObjectGroup = map:getObjectGroup("objects");
    local markPoint = getObjectGroup:getObject("transformdoor");
    local x = markPoint.x;
    local y = markPoint.y;
    local sprite = cc.Sprite:create(mgrs.res:getPngPath("transparent"));
    sprite:setPosition(cc.p(x,y));
    
    mgrs.action:beginAction(sprite):PlayAnimate("sendPoint",true):endAction();
    layer:addChild(sprite);

    local hero = require(SCRIPTS_DIR .. ".pubs.Hero").new();
    hero:setPosition(cc.p(200,200));
    layer:addChild(hero);
    self.hero = hero;
    
    local guild = cc.Sprite:create();
    guild:retain();
    layer:addChild(guild);
    self.guild = guild;
    mgrs.action:beginAction(guild):PlayAnimate("guide"):endAction();

    layer:setTouchEnabled(true);
    layer:addNodeEventListener(cc.NODE_TOUCH_EVENT,handler(self,self.onTouch));
    layer:scheduleUpdate();
    layer:addNodeEventListener(cc.NODE_ENTER_FRAME_EVENT,handler(self,self.update));
end

function MapWnd:onTouch(event)
    local layer = self.layer;
    local hero = self.hero;
    local guild = self.guild;
    local pos = layer:convertToNodeSpace(cc.p(event.x,event.y));
    self.m_Touch_Point = pos;
    if event.name  == "began" and mgrs.map:callWalk(pos) then
        if guild then
            guild:stopAllActions();
            guild:removeFromParent();
            mgrs.action:beginAction(guild):PlayAnimate("guide"):CallFunc(function()
                guild:removeFromParent();
            end):endAction();
            guild:setPosition(pos);
            layer:addChild(guild);
        end
        local path = mgrs.map:findWay(cc.p(hero:getPositionX(),hero:getPositionY()),pos);
        if path ~= 0 and path ~= -1 then
            print("sucess");
            dump(path);
            hero:setPath(path);
        end
        return true;
    end
end

function MapWnd:computePath(src,des)
    --self.hero:clearPath();

end

function MapWnd:update(dt)
    if self.hero and self.hero:heroUpdate(dt) then
        
    end
    --[[
    if self.m_Touch_Point.x ~= -1 and self.m_Touch_Point.y ~= -1 then
        if self.hero then
            self.hero:clearPath();
            self:computePath(self.hero:getPosition(),self.m_Touch_Point);
        end
        self.m_Touch_Point = cc.p(-1,-1);
    end
    --]]
    if self.hero then
        mgrs.map:setViewPort(cc.p(self.hero:getPositionX(),self.hero:getPositionY()));
    end
    --self.m_Touch_Point = cc.p(-1,-1);
end

function MapWnd:onClose()
	mgrs.animate:removeAnimationByName("sendPoint");
    mgrs.animate:removeAnimationByName("attack_stand");
    mgrs.animate:removeAnimationByName("guide");
end

function MapWnd:btnClicked()
    mgrs.log:printDebug("!!!!!TestWnd onClose!!!!!");
end



function MapWnd:reqData(handler)
	handler(true);	
end

function MapWnd:getRelativePosition()
	return {rx = 0.5; ry = 0.5; ox = mgrs.wnd.visibleRect.center.x; oy = mgrs.wnd.visibleRect.center.y;	};
end

function MapWnd:getLayer()
	return enums.Layer.Main;
end

function MapWnd:getPreloadResList()
	return {
        Plist = {"guide","transformdoor","attack","attack_stand","normal_stand","normal_walk","victory"}
    };
end

function MapWnd:getExclusiveType()
	return 0;
end

function MapWnd:isOpenCloseSoundEnabled()
	return false;
end

function MapWnd:onEnter()
	print("LoginWnd:onEnter");
end
function MapWnd:onExit()
	print("LoginWnd:onExit");
end
function MapWnd:onEnterTransitionFinish()
	print("LoginWnd:onEnterTransitionFinish");
end

function MapWnd:onExitTransitionStart()
	print("LoginWnd:onExitTransitionStart");
end

function MapWnd:onCleanup()
	print("LoginWnd:onCleanup");
end


mgrs.wnd:registerWnd(enums.Wnd.Map,MapWnd);

--endregion


--endregion
